﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //WriteGiftInfoXml();
        DeleteXml();
    }

    protected void WriteGiftInfoXml() {
        XmlDocument XDoc = new XmlDocument();
        XDoc.LoadXml("<color></color>");

        //设置版本信息
        XmlDeclaration xmldecl;
        xmldecl = XDoc.CreateXmlDeclaration("1.0", "utf-8", null);

        XmlElement root = XDoc.DocumentElement;
        XDoc.InsertBefore(xmldecl, root);

        //设置根节点
        XmlElement newGiftInfto = XDoc.DocumentElement;
        //创建礼品ID的节点
        XmlElement newGiftId = XDoc.CreateElement("GiftId");
        newGiftId.InnerText = "1";
        newGiftInfto.AppendChild(newGiftId);

        //创建礼品分类ID的节点
        XmlElement newCategoryId = XDoc.CreateElement("CategoryId");
        newCategoryId.InnerText = "Bless";
        newGiftInfto.AppendChild(newCategoryId);

        //XmlTextWriter tw = new XmlTextWriter("XML/test.xml", System.Text.Encoding.UTF8);
        //XDoc.WriteContentTo(tw);
        XDoc.Save(HttpContext.Current.Server.MapPath("XML/test.xml"));
        //tw.Close();
    }

    protected void DeleteXml() {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("XML/Color.xml"));
        xmlDoc.DocumentElement.RemoveAll();
        xmlDoc.Save(Server.MapPath("XML/Color.xml"));
    }
}
